package com.cbs.android.component.runtimecache.sample;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;

import com.cbs.android.component.log.L;
import com.cbs.android.component.runtimecache.RuntimeCache;

import java.util.List;

public class MainActivity extends AppCompatActivity {
    private static final String TAG = MainActivity.class.getName();
    private RuntimeCache runtimeCache;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        runtimeCache = new RuntimeCache(this, "test");
    }

    public void setValue(View view) {
        List<String> res = (List<String>) runtimeCache.getValue(TestCache.class);
        res.add(System.currentTimeMillis() + "");
        runtimeCache.setValue(TestCache.class, res);
    }

    public void getValue(View view) {
        List<String> res = (List<String>) runtimeCache.getValue(TestCache.class);
        L.e(TAG, res.size() + "");
    }

    public void concurrentTest(View view) {
        GetThread getThread1 = new GetThread();
        GetThread getThread2 = new GetThread();
        SetThread setThread1 = new SetThread();
        SetThread setThread2 = new SetThread();
        getThread1.start();
        setThread1.start();
        getThread2.start();
        setThread2.start();
    }

    private class GetThread extends Thread {
        @Override
        public void run() {
            List<String> res = (List<String>) runtimeCache.getValue(TestCache.class);
            L.e(TAG, res.size() + "");

        }
    }

    private class SetThread extends Thread {
        @Override
        public void run() {
            List<String> res = (List<String>) runtimeCache.getValue(TestCache.class);
            res.add(System.currentTimeMillis() + "");
            runtimeCache.setValue(TestCache.class, res);
        }
    }
}
